<?php

namespace App\Http\Controllers\Admin\Pages;

use App\Facades\DeviceService;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\DeviceRecord;
use Illuminate\Http\Request;
use Exception;

class DeviceController extends Controller
{   
    //设备列表
    public function index(Request $request)
    {
        $rules = [
            'type_id' => 'exists:device_types,id',
        ];
        $this->validate($request, $rules);

        $status = $this->getStatus($request);
        
        $deviceCode = $request->device_code;
        $deviceCode = str_replace('%', '\%', $deviceCode);
        $deviceCode = str_replace('_', '\_', $deviceCode);

        $deviceList = DeviceService::getDeviceRecordLlist($request->type_id, $deviceCode, $status);
        $deviceTypes = DeviceService::getALLType();
        return view('admin/deviceList')->with([
            'device_list' => $deviceList,
            'device_code' => $request->device_code,
            'status' => $request->status,
            'type_id' => $request->type_id,
            'device_types' => $deviceTypes,
            ]);
    }
    //历史记录
    public function history(Request $request)
    {
        $rules = [
            'device_id' => 'required|exists:device_records,device_id',
        ];
        $this->validate($request, $rules);

        $status = $this->getStatus($request);
        $deviceRecords = DeviceService::getDeviceRecordsByDeviceId($request->device_id, $status);
        // dd($deviceRecords);
        return view('admin/history')->with([
            'device_records' => $deviceRecords,
            'status' => $request->status,
            'device_id' => $request->device_id,
            ]);
    }

    public function getStatus($request)
    {
        if($request->status == '缺水') {
            $status = \App\Models\DeviceRecord::HYDROPENIA;
        } else if($request->status == '检修'){
             $status = \App\Models\DeviceRecord::OVERHAUL;
        } else {
            $status = '';
        }
        return $status;
    }

    

}
